package com.example.ai.service.impl;

import com.baidu.aip.face.AipFace;
import com.example.ai.service.AiFaceService;
import com.example.ai.service.dto.FaceItem;
import lombok.Data;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author : zhayh
 * @date : 2021-4-21 20:58
 * @description :
 */

@Data
@Service
@ConfigurationProperties(prefix = "baidu.ai")
public class AiFaceServiceImpl implements AiFaceService {
    private String appId;
    private String apiKey;
    private String secret;
    private String groupId;

    @Override
    public List<FaceItem> detectImageBase64(String imageBase64, String maxNum) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("face_field", "age,beauty,expression,faceshape,gender,glasses");
        options.put("max_face_num", "10");
        options.put("face_type", "LIVE");

        String image = imageBase64;
        String imageType = "BASE64";

        // 人脸检测
        AipFace client = getClient();
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res.toString(2));

        if (res.has("error_code") && res.getInt("error_code") == 0) {
            JSONObject result = res.getJSONObject("result");
            if (result.has("face_num") && result.getInt("face_num") > 0) {
                JSONArray faceList = result.getJSONArray("face_list");
                List<FaceItem> faceItems = new ArrayList<>();
                for (int i = 0; i < faceList.length(); i++) {
                    FaceItem faceItem = new FaceItem();
                    JSONObject faceJson = faceList.getJSONObject(i);
                    faceItem.setFaceToken(faceJson.getString("face_token"));
                    faceItem.setAge(faceJson.getDouble("age"));
                    faceItem.setBeauty(faceJson.getInt("beauty"));

                    JSONObject expression = faceJson.getJSONObject("expression");
                    faceItem.setExpression(expression.getString("type"));

                    JSONObject gender = faceJson.getJSONObject("gender");
                    faceItem.setGender(gender.getString("type"));

                    JSONObject glasses = faceJson.getJSONObject("glasses");
                    faceItem.setGlasses(glasses.getString("type"));

                    JSONObject faceShape = faceJson.getJSONObject("face_shape");
                    faceItem.setFaceShape(faceShape.getString("type"));
                    faceItem.setFaceToken(faceJson.getString("face_token"));
                    faceItems.add(faceItem);
                }
                return faceItems;
            }
        }
        return null;
    }

    @Override
    public String searchFace(String faceToken) {
        HashMap<String, String> options = new HashMap<>();
        options.put("quality_control", "NORMAL");
        String image = faceToken;
        String imageType = "FACE_TOKEN";
        String groupIds = groupId;

        // 人脸搜索
        AipFace client = getClient();
        JSONObject res = client.search(image, imageType, groupIds, options);
        System.out.println(res.toString(2));
        if (res.has("error_code") && res.getLong("error_code") == 0) {
            JSONObject resResult = res.getJSONObject("result");
            //解析人脸搜索结果
            JSONArray userList = resResult.getJSONArray("user_list");
            if (userList != null && userList.length() > 0) {
                JSONObject userJson = userList.getJSONObject(0);
                //得到人脸识别结果
                return userJson.getString("user_id");
            }
        }
        return null;
    }

    public AipFace getClient() {
        return new AipFace(appId, apiKey, secret);
    }
}
